//
//  ProPayDetailController.m
//  PropertyMananger
//
//  Created by Mansion on 17/3/5.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "ProPayDetailController.h"
#import "PropertyPayDetailCell.h"
#import "LeaderModel.h"

@interface ProPayDetailController ()

@property (nonatomic,copy) NSString * monthStr;//系统月份
@property (nonatomic,strong) PropertyPayDetailCell * proPayDetcell;

@property (nonatomic,copy) NSString * yearStr;
@property (nonatomic,strong) NSMutableArray * leaderArray;

@end

@implementation ProPayDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getCalendar];
    
    //上一级传来的时间
     self.yearStr = self.selectedTime;
    NSArray * timeArr = [self.selectedTime componentsSeparatedByString:@"-"];
    self.monthStr = timeArr[1];
    //系统默认时间
  //  [self getYearAndMonthData];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 50, BHscreenW, BHscreenH - 64 - 50) isGroup:NO];
    
    self.leaderArray = [[NSMutableArray alloc]init];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(monthStrNoti:) name:MonthNoticeCenter object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(timeStrNoti:) name:timeStrNoticeCenter object:nil];
    
    [self postLeaderFromData];
    
}
#pragma mark - 接收时间的通知
- (void)monthStrNoti:(NSNotification *)noti{

    DLog(@"时间 === %@",noti.userInfo[@"key"]);
    self.monthStr = noti.userInfo[@"key"];
   self.proPayDetcell.titleLabel.text = [NSString stringWithFormat:@"%@月物业缴费金额",self.monthStr];
}
- (void)timeStrNoti:(NSNotification *)noti{

    self.yearStr = noti.userInfo[@"key"];

}
- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   self.proPayDetcell = [PropertyPayDetailCell cellWithTableView:tableView];
    
      self.proPayDetcell.titleLabel.text = [NSString stringWithFormat:@"%@月物业缴费金额",self.monthStr];
    if (self.leaderArray.count != 0) {
        
        LeaderModel * model = self.leaderArray[0];
        
        self.proPayDetcell.model = model;
    }
  
    
    return self.proPayDetcell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 215;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
}
#pragma mark - 领导报表数据请求
- (void)postLeaderFromData{
    
    NSDictionary * parms = @{
                             @"Cid":userModel.CommunityID,
                             @"MonthTime":self.yearStr,
                             @"json":@"\{\"jsonEntity\":\{},\"action\":\"getallreport\",\"version\":\"V1.0\"}"};
    DLog(@"领导提交的参数===%@",parms);
    
    [HomeNetWorkRequest postLeaderFromSuccess:^(id data) {
        
        [self.leaderArray removeAllObjects];
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary * dict1 = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"领导报表数据 == %@",dict1);
        
        DLog(@"%@",dict[@"Msg"]);
        
        LeaderModel * model = [[LeaderModel alloc]init];
        [model setValuesForKeysWithDictionary:dict1];
        
        DLog(@"%@",model.Complainall);
        [self.leaderArray addObject:model];
        
        //方法一：直接把里面的model赋给他,方法二在LeaderModel里
        //        MonthModel * monthModel = [[MonthModel alloc]init];
        //
        //        [monthModel setValuesForKeysWithDictionary:dict1[@"MonthReportdata"]];
        //
        //        [self.leaderArray addObject:monthModel];
        //
        //        model.monthModel = monthModel;
        
        [self.tableView reloadData];
        
    } Fail:^(NSString *errorDes) {
        
    } param:parms];
    
    
}
#pragma mark - 获取系统时间
- (void)getYearAndMonthData{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSString * timeDataStr = [NSString stringWithFormat:@"%@",dateNow];
    DLog(@" === %@",dateNow);
    
    NSArray * arr = [timeDataStr componentsSeparatedByString:@"+"];
    DLog(@"日期格式化 == %@",arr[0]);
    
    NSArray * timeArr = [timeDataStr componentsSeparatedByString:@"-"];
    NSString * timeStr = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
    DLog(@"拼接好的时间===%@",timeStr);
    self.yearStr = timeStr;
}

-(void)getCalendar{
    
    NSDate* nowTime = [NSDate date];
    
    NSCalendar * cal= [NSCalendar currentCalendar];
    
    unsigned int unitFlags= NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    
    NSDateComponents* dd = [cal components:unitFlags fromDate:nowTime];
    
    DLog(@"月份 = %ld",(long)dd.month);
    
    self.monthStr = [NSString stringWithFormat:@"%ld",dd.month];
    
}


@end
